#include <QCoreApplication>
#include <QTextCodec>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QTime>
#include <QtSql/QSqlError>
#include <QtDebug>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlRecord>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //设置中文显示
    QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("easybook-testdemo");    //设置数据库主机名
    db.setDatabaseName("qtDB.db");      //设置数据库名称
    db.setUserName("zhangsan");         //设置数据库用户名
    db.setPassword("123123");           //设置用户名的密码
    bool success = db.open();      //打开连接
    if(!success)
    {
        QSqlError lastError = db.lastError();
        qDebug() << lastError.driverText() << QString(QObject::tr("Open Connect!"));
    }

    //创建数据库表
    QSqlQuery query;
    success = query.exec("CREATE TABLE automobile ("
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    "attribute VARCHAR(40) NOT NULL, "
                    "type VARCHAR(40) NOT NULL, "
                    "kind VARCHAR(40) NOT NULL, "
                    "nation INTEGER, "
                    "carnumber INTEGER, "
                    "elevaltor INTEGER, "
                    "distance INTEGER, "
                    "oil INTEGER, "
                    "temperature INTEGER)");

    if(success)
        qDebug() << QObject::tr("Data Base Table Create Success!");
    else
        qDebug() << QObject::tr("Create Fail") << query.lastError().driverText();

    //查询
    query.exec("select * from automobile");
    QSqlRecord rec = query.record();
    qDebug() << QObject::tr("automobile Table Keys Numbers:") << rec.count();

    //插入数据
    QTime t;
    t.start();
    query.prepare("insert into automobile values(?,?,?,?,?,?,?,?,?,?)");

    long records = 100;
    for(int i = 0; i < records; i++)
    {
        query.bindValue(0, i);
        query.bindValue(1, "silun");
        query.bindValue(2, "jiaoche");
        query.bindValue(3, "fukang");
        query.bindValue(4, rand()%100);
        query.bindValue(5, rand()%10000);
        query.bindValue(6, rand()%300);
        query.bindValue(7, rand()%200000);
        query.bindValue(8, rand()%52);
        query.bindValue(9, rand()%100);

        success = query.exec();
        if(!success)
        {
            QSqlError lastError = query.lastError();
            qDebug() << lastError.driverText() << QString(QObject::tr("Insert Fail"));
        }
    }
    qDebug() << QObject::tr("Insert %1 Count, Times：%2 ms").arg(records).arg(t.elapsed());

    //排序
    t.restart();
    success = query.exec("select * from automobile order by id desc");
    if(success)
        qDebug() << QObject::tr("Sort %1 Count, Times：%2 ms").arg(records).arg(t.elapsed());
    else
        qDebug() << QObject::tr("Sort Fail!");

    //更新记录
    t.restart();
    for(int i = 0; i < records; i++)
    {
        query.clear();
        query.prepare(QString("update automobile set attribute=?,type=?,"
                              "kind=?,nation=?,"
                              "carnumber=?,elevaltor=?,"
                              "distance=?,oil=?,"
                              "temperature=? where id = %1").arg(i));
        query.bindValue(0, "silun");
        query.bindValue(1, "jiaoche");
        query.bindValue(2, "fukang");
        query.bindValue(3, rand()%100);
        query.bindValue(4, rand()%10000);
        query.bindValue(5, rand()%300);
        query.bindValue(6, rand()%200000);
        query.bindValue(7, rand()%52);
        query.bindValue(8, rand()%100);
        success = query.exec();
        if(!success)
        {
            QSqlError lastError = query.lastError();
            qDebug() << lastError.driverText() << QString(QObject::tr("Update Fail"));
        }
    }

    qDebug() << QObject::tr("Update %1 Count, Times：%2 ms").arg(records).arg(t.elapsed());

    //删除
    t.restart();
    query.exec("delete from automobile where id=15");

    //输出操作耗时
    qDebug() << QObject::tr("Delete one count, Times：%1 ms").arg(t.elapsed());


    return a.exec();
}
